package org.example.judge_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.example.judge_admin.entitry.RequestSessionParams;
import org.example.judge_admin.mapper.SessionMapper;
import org.example.judge_admin.service.SessionService;
import org.example.judge_common.entity.PageResult;
import org.example.judge_common.entity.RequestParams;
import org.example.judge_pojo.Session;
import org.example.judge_pojo.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("SessionServiceImpl")
public class SessionServiceImpl extends ServiceImpl<SessionMapper, Session> implements SessionService {
    @Override
    public PageResult<Session> search(RequestSessionParams params) {
         QueryWrapper<Session> wrapper = new QueryWrapper<Session>().eq("user_id", params.getUserId());
        //排序
        wrapper.orderBy(true,false,"create_time");
        //设置分页
        PageHelper.startPage(params.getPage(), params.getSize(),false);
        List<Session> list = super.list(wrapper);
        //求数据条数
        Integer count = this.baseMapper.selectCount(wrapper);
        return new PageResult<Session>(count.longValue(),list);
    }

    @Override
    public Session getLastOne(String userId) {

        QueryWrapper<Session> wrapper = new QueryWrapper<Session>().eq("user_id",userId);
        //排序
        wrapper.orderBy(true,false,"create_time");
        //设置分页
        PageHelper.startPage(1, 1,false);
        List<Session> list = super.list(wrapper);
        if(list==null||list.size()==0)return null;
        return list.get(0);
    }
}
